package com.start.autumn.retry;

import feign.RetryableException;
import feign.Retryer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
public class RetryerConfig implements Retryer {
    /**
     * 最大尝试次数
     */
    private int maxAttempts;

    /**
     * 重试间隔时间
     */
    private long backoff;

    /**
     * 当前重试次数
     */
    private int attempt;

    @Override
    public void continueOrPropagate(RetryableException e) {
        if (attempt++ >= maxAttempts) {
            throw e;
        }
        log.error("==> Openfeign Retry frequency: {}, Retry interval: {}", attempt, backoff);
    }

    @Override
    public Retryer clone() {
        return new RetryerConfig(maxAttempts, backoff, 1);
    }
}
